package shuzi.activity;

import java.util.ArrayList;
import java.util.List;

import shuzi.adapter.BrandAdapter;
import shuzi.data.Database;
import shuzi.model.Brand;
import shuzi.widget.MyGridView;
import shuzi.widget.TopBarRelativeLayout;
import shuzi.widget.TopBarRelativeLayout.OnBarClickListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.PauseOnScrollListener;
import com.shuzi.offer.R;

/**
 * @author lung 下午11:10:23 2013
 */
public class MainActivity extends Activity implements OnItemClickListener {
      
	/** 标题栏 */
	private TopBarRelativeLayout topbar = null;
	/** 滚动 */
	private ProgressBar progressBar = null;
	/** 九宫格布局 */
	private MyGridView gridView = null;
	/** 适配器 */
	private BrandAdapter adapter = null;
	/** 刷新界面的handler */
	private Handler handler = null;
	/** 数据列表 */
	private List<Brand> listAll = null;
	/** 是否点击了售后 */
	private boolean isAfterSale = false;
	private ImageLoader imageLoader = ImageLoader.getInstance();


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			System.exit(0);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initHandler();
		initLayout();
		initDada();

	}

	/**
	 * 初始化handler
	 */
	@SuppressLint("HandlerLeak")
	private void initHandler() {
		handler = new Handler() {
			
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case Database.HANDLER_REFRESH_GRIDVIEW:
					// listAll = (List<Brand>) msg.obj;
					refreshGridView();
					break;
				case Database.HANDLER_REFRESH_ERROR:
					progressBar.setVisibility(View.GONE);
					Toast.makeText(MainActivity.this, "发生错误", Toast.LENGTH_LONG)
							.show();
					break;
				case Database.HANDLER_REFRESH_NODATA:
					progressBar.setVisibility(View.GONE);
					Toast.makeText(MainActivity.this, "暂无数据",
							Toast.LENGTH_LONG).show();
					break;

				default:
					break;
				}
			}
		};

	}

	/**
	 * 初始化布局
	 */
	private void initLayout() {
		topbar = (TopBarRelativeLayout) findViewById(R.id.topBar);
		gridView = (MyGridView) findViewById(R.id.mainGridView);
		progressBar = (ProgressBar) findViewById(R.id.progressBar);
		topbar.setOnBarClickListener(new OnBarClickListener() {

			@Override
			public void onBarClick(int id) {
				//Toast.makeText(MainActivity.this, id + "", Toast.LENGTH_SHORT);
				Intent intent = new Intent();
				switch (id) {
				case R.id.otherBrands:
					if(!isAfterSale){
						topbar.otherBrands.setBackgroundDrawable(getResources().getDrawable(R.drawable.title_press_button));
					}else{
						topbar.otherBrands.setBackgroundDrawable(getResources().getDrawable(R.drawable.title_button));
						
					}
					isAfterSale = !isAfterSale;
					break;
				case R.id.recommendToady:
					intent.setClass(MainActivity.this, PhoneInfoActivity.class);
					intent.putExtra(Database.IS_RECOMMEND, true);
					startActivity(intent);
				default:
					break;
				}

			}
		});
	}

	/**
	 * 初始化数据
	 */
	private void initDada() {
		listAll = new ArrayList<Brand>();
		if (Database.brandList != null) {
			listAll = Database.brandList;
			handler.sendEmptyMessage(Database.HANDLER_REFRESH_GRIDVIEW);
		}

	}

	/**
	 * 刷新视图
	 */
	private void refreshGridView() {
		progressBar.setVisibility(View.GONE);
		adapter = new BrandAdapter(this, listAll);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(this);
		gridView.setOnScrollListener(new PauseOnScrollListener(imageLoader, false, false));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (isAfterSale&&listAll!=null) {
			Intent intent = new Intent();
			intent.setClass(MainActivity.this, AfterSaleActivity.class);
			intent.putExtra(Database.brandId, listAll.get(position).getId());
			startActivity(intent);
		} else {

			if (listAll != null) {
				Intent in = new Intent();
				in.putExtra(Database.typeId, listAll.get(position)
						.getBrandTypeCode());
				in.putExtra(Database.brandId, listAll.get(position).getId());
				in.putExtra(Database.name, listAll.get(position)
						.getBrandTypeName());
				in.setClass(this, PhoneInfoActivity.class);
				startActivity(in);
			}
		}
	}

}
